package com.example.afinal;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyThread implements Runnable{
    private static final String TAG = "MyThread";
    Handler handler;

    public void setHandler(Handler handler){
        this.handler = handler;
    }
    @Override
    public void run() {
        Log.i(TAG,"run: ()..................");

        try {
            Thread.sleep(3000);
        }catch (InterruptedException e){
            throw new RuntimeException(e);
        }
        List<String> retlist = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect("https://www.huilvzaixian.com/").get();
            Elements uls = doc.getElementsByTag("ul");
            Element ul4 = uls.get(3);
//            Log.i(TAG,"run: table2=" + table2);
            Elements lis = ul4.getElementsByTag("li");
            for (Element li : lis){
                Elements as = li.getElementsByTag("a");
                // Log.i(TAG,"run: tds=" + tds);
                for(Element a: as){
                    String r = a.text();
                    String rate = r.substring(3);
                    Log.i(TAG,"run: " + rate);
                    retlist.add(rate);
                }

//                for (int i = 0;i<as.size();i++){
//                    String rate = as.get(i).text();
//                    Log.i(TAG,"run: " + rate);
//                    retlist.add(rate);
//                }


            }


        } catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }

        //发送数据回主线程
        Message msg = handler.obtainMessage(7,retlist);
        msg.obj = retlist;
        handler.sendMessage(msg);
        Log.i(TAG,"run: msg已发送");
    }
}
